package dyyx.idle;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.Channel;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.logging.LogLevel;
import io.netty.handler.logging.LoggingHandler;
import io.netty.handler.timeout.IdleStateHandler;
import io.netty.util.concurrent.DefaultThreadFactory;

public class IdleEchoServer {

	
    static final int port = Integer.parseInt(System.getProperty("port", "6789"));
    static final int bossThreads = Integer.parseInt(System.getProperty("bossThreads", "1"));
    static final int workerThreads = Integer.parseInt(System.getProperty("workerThreads", "3"));

   


	public static Map<String,Object> getInfo(){
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("port", port);
		map.put("bossThreads", bossThreads);
		map.put("workerThreads", workerThreads);
		


		return map;
	}
	
	
	public static void main(String[] args) throws Exception {
		
		System.out.println("echo server start "+System.currentTimeMillis());
		System.out.println(getInfo());

     
        EventLoopGroup bossGroup = new NioEventLoopGroup(bossThreads,new DefaultThreadFactory("bossGroup"));
        EventLoopGroup workerGroup = new NioEventLoopGroup(workerThreads,new DefaultThreadFactory("bossGroup"));
       
        try {
            ServerBootstrap b = new ServerBootstrap();
            b.option(ChannelOption.SO_BACKLOG, 1024);
            b.option(ChannelOption.TCP_NODELAY, true);
            b.childOption(ChannelOption.TCP_NODELAY, true);

            
            b.group(bossGroup, workerGroup)
            // b.group(singleGroup, singleGroup)
             .channel(NioServerSocketChannel.class)
             // 去掉这个 启动时不会打印日志
             .handler(new LoggingHandler(LogLevel.INFO))
             // .handler(new LoggingHandler(LogLevel.DEBUG))
             .childHandler(new ChannelInitializer<SocketChannel>() {
                 @Override
                 public void initChannel(SocketChannel ch) {
                     ChannelPipeline p = ch.pipeline();
                     // IdleStateHandler(long readerIdleTime, long writerIdleTime, long allIdleTime,TimeUnit unit)
                     // this(false, readerIdleTime, writerIdleTime, allIdleTime, unit)
                     p.addLast(new IdleStateHandler(5, 7, 20,TimeUnit.SECONDS));

                     p.addLast(new IdleEchoServerHandler());
                 }
             });

            Channel ch = b.bind(port).sync().channel();

            System.out.println("start done,port="+port);
    		System.out.println(getInfo());

            
            ch.closeFuture().sync();
        } finally {
            bossGroup.shutdownGracefully();
            workerGroup.shutdownGracefully();
        }
    }

		

	
}
